package com.hotelManagerSystem.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotelManagerSystem.model.CleaningRecord;
import com.hotelManagerSystem.model.Staff;
import com.hotelManagerSystem.service.ICleaningRecordService;
import com.hotelManagerSystem.service.IRoomService;
import com.hotelManagerSystem.service.IStaffService;
import com.hotelManagerSystem.util.PageUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author treasy
 * @since 2021-12-22
 */
@Controller
@RequestMapping("/cleaning-record")
public class CleaningRecordController {
    /**
     * @author treasy
     * @description 查询所有清理记录
     * */
    @Autowired
    ICleaningRecordService iCleaningRecordService;
    @Autowired
    IStaffService iStaffService;
    @Autowired
    IRoomService iRoomService;

    @RequestMapping("/admin/list")
    public String queryAllCleanRecord(Model model,
                                      @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                      @RequestParam(defaultValue="8",value="pageSize")Integer pageSize,
                                      @RequestParam(required = false,value="cleaningState")Integer cleaningState){
        PageUtil.runStartPage(pageNum,pageSize);
        List<CleaningRecord> cleaningRecords = null;
        if (cleaningState==null){
            cleaningRecords = iCleaningRecordService.queryAll();
        }else {
            cleaningRecords = iCleaningRecordService.queryInfoByState(cleaningState);
        }
        PageInfo pageInfo = PageUtil.getPageInfo(cleaningRecords);
        model.addAttribute("pageInfo",pageInfo);
        return "/admin/cleanPage/clean-info";
    }

    /**
     * @author treasy
     * @param model
     * @return
     */
    @RequestMapping("/admin/staffMission")
    public String staffMissionList(Model model,
                                   @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                   @RequestParam(defaultValue="8",value="pageSize")Integer pageSize,
                                   @RequestParam(required = false,value="cleaningState")Integer cleaningState){
        //获取当前用户Subject
        Subject currentUser = SecurityUtils.getSubject();
        //获取当前用户username
        String username = (String) currentUser.getSession().getAttribute("username");
        //根据用户名获取staffId
        Staff staff = iStaffService.queryByUsername(username);
        PageUtil.runStartPage(pageNum,pageSize);
        List<CleaningRecord> cleaningRecords = null;
        if (cleaningState==null){
            cleaningRecords = iCleaningRecordService.queryInfoByStaffId(staff.getStaffId());
        }else {
            cleaningRecords = iCleaningRecordService.queryInfoByStaffIdAndState(staff.getStaffId(),cleaningState);
        }
        PageInfo pageInfo = PageUtil.getPageInfo(cleaningRecords);
        model.addAttribute("pageInfo",pageInfo);
        return "/admin/cleanPage/mission-list";
    }

    //-----查询-----房间打扫记录列表-----
    @RequestMapping("/list")
    public String list(Model model){
        //内置的方法list
        List<CleaningRecord> cleaningRecordList_db=iCleaningRecordService.list();
        model.addAttribute("cleaningRecordList_db",cleaningRecordList_db);
        System.out.println("===查询房间打扫记录列表===list()===");
        return "system/admin/cleaningRecord/cleaningRecordList";
    }

    /**
     * @author treasy
     * @description 根据记录状态查询记录
     * @param model
     * @param cleaningState
     * @return
     */
    @RequestMapping("/admin/getByState")
    public String queryInfoByState(Model model,int cleaningState){
        List<CleaningRecord> cleaningRecords = iCleaningRecordService.queryInfoByState(cleaningState);
        model.addAttribute("cleaningRecords",cleaningRecords);
        return "/admin/cleanPage/clean-info";
    }
    /**
     * @author treasy
     * @description 根据订单id插入打扫员工id
     */
    @RequestMapping("/admin/insertStaffId")
    @ResponseBody
    public Map<String, Object> insertCleanRecordWithStaffId(int cleaningRecordId,int staffId){
        Map<String, Object> map=new HashMap<>();
        //根据清理记录id插入清洁员工id并更新记录状态
        int flag = iCleaningRecordService.updateRecordById(cleaningRecordId,staffId);
        //根据订单id更新房间状态
        CleaningRecord cleaningRecord = iCleaningRecordService.getById(cleaningRecordId);
        iRoomService.updateStateById(cleaningRecord.getRoomId(),0);
        if (flag==1){
            map.put("code",200);
            map.put("message","添加成功");
        }else {
            map.put("code",500);
            map.put("message","添加失败");
        }
        return map;
    }
    /*-----添加UI-----房间打扫记录-----*/
    @RequestMapping("/addUI")
    public String addUI(Model model){
        System.out.println("===添加UI===addUI()===");
        return "system/admin/cleaningRecord/addCleaningRecord";
    }


    @RequestMapping("/admin/updateRecordState")
    @ResponseBody
    public Map<String, Object> updateRecordState(int cleaningRecordId){
        Map<String, Object> map=new HashMap<>();
        Date confirmTime = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String cleanOverTime=simpleDateFormat.format(confirmTime);
        //更新打扫信息状态
        int flag = iCleaningRecordService.updateRecordState(cleaningRecordId,cleanOverTime);
        //更新房间状态
        CleaningRecord cleaningRecord = iCleaningRecordService.getById(cleaningRecordId);
        int flag1 = iRoomService.updateStateByIdClean(cleaningRecord.getRoomId());
        if(flag==1){
            map.put("code",200);
            map.put("message","更新成功");
        }else {
            map.put("code",500);
            map.put("message","更新失败");
        }
        return map;
    }
//    -----添加-----房间打扫记录
    @RequestMapping("/add")
    public String add(Model model,CleaningRecord cleaningRecord,String cleaningTime,String overTime) throws ParseException {
        //获取开始打扫时间，打扫结束时间
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date localDateTime = simpleDateFormat.parse(cleaningTime);
        cleaningRecord.setRecordTime(localDateTime);

        localDateTime=simpleDateFormat.parse(overTime);
        cleaningRecord.setCleanOverTime(localDateTime);
        System.out.println(cleaningRecord);

        //调用业务
        boolean b=iCleaningRecordService.save(cleaningRecord);
        //返回cleaningRecordList页面
        List<CleaningRecord> cleaningRecordList_db=iCleaningRecordService.list();
        model.addAttribute("cleaningRecordList_db",cleaningRecordList_db);
        System.out.println("===添加房间打扫记录");
        return "system/admin/cleaningRecord/cleaningRecordList";
    }

    /*-----删除-----通过cleaningRecordId删除房间打扫记录信息-----*/
    @RequestMapping("/delete")
    public String delete(Model model,int cleaningRecordId){
        //获取数据
        System.out.println(cleaningRecordId);
        //调用业务
        boolean b=iCleaningRecordService.removeById(cleaningRecordId);
        //返回roomOrderList页面
        List<CleaningRecord> cleaningRecordList_db=iCleaningRecordService.list();
        model.addAttribute("cleaningRecordList_db",cleaningRecordList_db);
        System.out.println("===删除房间打扫记录信息===delete()===");
        return "system/admin/cleaningRecord/cleaningRecordList";
    }

    /*-----修改UI-----房间打扫记录信息-----*/
    @RequestMapping("/updateUI")
    public String updateUI(Model model,int cleaningRecordId){
        //接收数据
        System.out.println(cleaningRecordId);
        //调用业务
        //通过 cleaningRecordId房间打扫记录ID查询对应的房间打扫记录信息
        CleaningRecord cleaningRecord_db=iCleaningRecordService.getById(cleaningRecordId);
        model.addAttribute("cleaningRecord_db",cleaningRecord_db);
        System.out.println("===修改UI===updateUI()===");
        //返回updateCleaningRecord页面
        return "system/admin/cleaningRecord/updateCleaningRecord";
    }

    /*-----修改-----房间打扫记录信息-----*/
    @RequestMapping("/update")
    public String update(Model model,CleaningRecord cleaningRecord,String cleaningTime,String overTime) throws ParseException {
        //获取开始打扫时间，打扫结束时间
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date localDateTime = simpleDateFormat.parse(cleaningTime);
        cleaningRecord.setRecordTime(localDateTime);

        localDateTime=simpleDateFormat.parse(overTime);
        cleaningRecord.setCleanOverTime(localDateTime);
        System.out.println(cleaningRecord);

        //调用业务进行更新
        boolean b=iCleaningRecordService.saveOrUpdate(cleaningRecord);
        //返回cleaningRecordList页面
        List<CleaningRecord> cleaningRecordList_db=iCleaningRecordService.list();
        model.addAttribute("cleaningRecordList_db",cleaningRecordList_db);
        System.out.println("===修改房间打扫记录信息===update()===");
        return "system/admin/cleaningRecord/cleaningRecordList";
    }
}
